﻿using System;
using System.IO;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Xml.Serialization;

namespace Triad.Common
{

    /// <summary>
    /// Partial store class for session variables shared between multiple "Views"
    /// </summary>
    /// <remarks>
    /// Author: patrick.konkle,   5/14/2008 - 10:53 AM
    /// Modified By: 
    /// </remarks>
    public partial class store
    {
        /// <summary>
        /// Serializes this instance.
        /// </summary>
        /// <param name="p_obj">The object</param>
        /// <param name="p_T">The type of object.</param>
        /// <param name="p_storeName">ID of the Session Variable to store the XML representation of the object into. If Null or Empty - does *not* store the tree in Session.</param>
        /// <returns>xml representation of the object</returns>
        /// <remarks>
        /// Author: tim.fischer,   8/11/2008 - 11:00 AM
        /// Modified By:
        /// </remarks>
        public static string Serialize(object p_obj, Type p_T, string p_storeName)
        {
            //Serialize
            StringWriter sw = new StringWriter();
            XmlSerializer s = new XmlSerializer(p_T);
            s.Serialize(sw, p_obj);

            if (p_storeName.Length > 0)
            {
                HttpContext.Current.Session[p_storeName] = sw.ToString();
            }

            return sw.ToString();
        }

        /// <summary>
        /// DeSerializes an XML string into a strongly typed object.
        /// </summary>
        /// <typeparam name="T">ClassName of the Object</typeparam>
        /// <param name="p_storeName">ID of Session Variable - if not null, return XML string from session.</param>
        /// <param name="p_xmlString">If storeName is null, use this XML string to populate the Object.</param>
        /// <returns>
        /// ProductTreeLine object populated via Session variable XML string
        /// </returns>
        /// <remarks>
        /// Author: tim.fischer,   8/11/2008 - 11:01 AM
        /// Modified By:
        /// </remarks>
        public static T DeSerialize<T>(string p_storeName, string p_xmlString) where T : class, new()
        {
            if (p_storeName.Length>0)
            {
                p_xmlString = (string)HttpContext.Current.Session[p_storeName];
            }
            StringReader sr = new StringReader(p_xmlString);
            XmlSerializer s = new XmlSerializer(typeof(T));
            var obj = s.Deserialize(sr);
            return obj as T;
        } 

        /// <summary>
        /// Session Variable properties for Shared
        /// </summary>
        /// <remarks>
        /// Author: patrick.konkle,   5/14/2008 - 10:53 AM
        /// Modified By: 
        /// </remarks>
        public class Shared
        {

            /// <summary>
            /// Clear a single session variable from the IIS Server's memory
            /// </summary>
            /// <param name="strName">Name of the STR.</param>
            /// <remarks>
            /// Author: patrick.konkle,   5/14/2008 - 10:53 AM
            /// Modified By: 
            /// </remarks>
            public static void Clear(string strName)
            {
                if (HttpContext.Current.Session[strName] != null)
                    HttpContext.Current.Session.Remove(strName);
            }

            /// <summary>
            /// Remove All Session Variables for class Shared
            /// </summary>
            /// <remarks>
            /// Author: patrick.konkle,   5/14/2008 - 10:52 AM
            /// Modified By: 
            /// </remarks>
            public static void ClearAll()
            {
                Clear("UserName");
                Clear("ITARFlag");
            }

            /// <summary>
            /// Gets the MSG box answer.
            /// </summary>
            /// <value>The MSG box answer.</value>
            /// <remarks>
            /// Author: patrick.konkle,   5/14/2008 - 10:52 AM
            /// Modified By: 
            /// </remarks>
            public static string MsgBoxAnswer
            {
                get
                {
                    object o = HttpContext.Current.Session["MsgBoxAnswer"];
                    return o == null ? "" : (string)o;
                }
 
            }

            /// <summary>
            /// Gets or sets the name of the user.
            /// </summary>
            /// <value>The name of the user.</value>
            /// <remarks>
            /// Author: patrick.konkle,   5/14/2008 - 10:52 AM
            /// Modified By: 
            /// </remarks>
            public static string UserName
            {
                get
                {
                    object o = HttpContext.Current.Session["UserName"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["UserName"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the ITAR Flag.
            /// </summary>
            /// <value>The ITARF lag.</value>
            /// <remarks>
            /// Author: patrick.konkle,   5/14/2008 - 10:51 AM
            /// Modified By: 
            /// </remarks>
            public static string ITARFLag
            {
                get
                {
                    object o = HttpContext.Current.Session["ITARFlag"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["ITARFlag"] = value;
                }
            }

            /// <summary>
            /// Gets or sets a single unit id.
            /// </summary>
            /// <value>The unit id.</value>
            /// <remarks>
            /// Author: patrick.konkle,   5/13/2008 - 3:13 PM
            /// Modified By: 
            /// </remarks>
            public static string UnitIdSingle
            {
                get
                {
                    object o = HttpContext.Current.Session["UnitIdSingle"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["UnitIdSingle"] = value;
                }
            }

            /// <summary>
            /// Gets or sets multiple unit ids.
            /// </summary>
            /// <value>The unit id multi.</value>
            /// <remarks>
            /// Author: patrick.konkle,   5/14/2008 - 10:49 AM
            /// Modified By: 
            /// </remarks>
            public static string UnitIdMulti
            {
                get
                {
                    object o = HttpContext.Current.Session["UnitIdMulti"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["UnitIdMulti"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the list of multiple unit ids.
            /// </summary>
            /// <value>The unit list multi.</value>
            /// <remarks>
            /// Author: patrick.konkle,   6/3/2008 - 11:25 AM
            /// Modified By: 
            /// </remarks>
            public static string UnitListMulti
            {
                get
                {
                    object o = HttpContext.Current.Session["UnitListMulti"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["UnitListMulti"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the type of the process.
            /// </summary>
            /// <value>The type of the process.</value>
            /// <remarks>
            /// Author: patrick.konkle,   4/24/2008 - 10:55 AM
            /// Modified By: 
            /// </remarks>
            public static string ProcessType
            {
                get
                {
                    object o = HttpContext.Current.Session["ProcessType"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["ProcessType"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the unit readiness collection.
            /// </summary>
            /// <value>The unit readiness collection.</value>
            /// <remarks>
            /// Author: patrick.konkle,   4/29/2008 - 4:43 PM
            /// Modified By: 
            /// </remarks>
            public static string UnitReadinessCollection
            {
                
                get
                {
                    object o = HttpContext.Current.Session["UnitReadinessCollection"];
                    return (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["UnitReadinessCollection"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the disambig rec.
            /// </summary>
            /// <value>The disambig rec.</value>
            /// <remarks>
            /// Author: patrick.konkle,   5/1/2008 - 1:12 PM
            /// Modified By: 
            /// </remarks>
            public static string DisambigRec
            {
                get
                {
                    object o = HttpContext.Current.Session["DisambigRec"];
                    return (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["DisambigRec"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the site id of the Unit (Product Owner).
            /// </summary>
            /// <value>The site id unit.</value>
            /// <remarks>
            /// Author: patrick.konkle,   6/6/2008 - 10:51 AM
            /// Modified By: 
            /// </remarks>
            public static string SiteIdUnit
            {
                get
                {
                    object o = HttpContext.Current.Session["SiteIdUnit"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["SiteIdUnit"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the focused factory of the unit (Product Owner).
            /// </summary>
            /// <value>The focused factory unit.</value>
            /// <remarks>
            /// Author: patrick.konkle,   6/6/2008 - 10:55 AM
            /// Modified By: 
            /// </remarks>
            public static string FocusedFactoryIdUnit
            {
                get
                {
                    object o = HttpContext.Current.Session["FocusedFactoryIdUnit"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["FocusedFactoryIdUnit"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the operation attributes.
            /// </summary>
            /// <value>The operation attributes.</value>
            /// <remarks>
            /// Author: patrick.konkle,   6/9/2008 - 4:52 PM
            /// Modified By: 
            /// </remarks>
            public static string OperationAttributes
            {

                get
                {
                    object o = HttpContext.Current.Session["OperationAttributes"];
                    return (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["OperationAttributes"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the enforcement types.
            /// </summary>
            /// <value>The enforcement types.</value>
            /// <remarks>
            /// Author: Patrick.Konkle,   6/10/2008 - 3:37 PM
            /// Modified By: 
            /// </remarks>
            public static string EnforcementTypes
            {
                get
                {
                    object o = HttpContext.Current.Session["EnforcementTypes"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["EnforcementTypes"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the data collection types.
            /// </summary>
            /// <value>The data collection types.</value>
            /// <remarks>
            /// Author: Patrick.Konkle,   6/10/2008 - 3:38 PM
            /// Modified By: 
            /// </remarks>
            public static string DataCollectionTypes
            {
                get
                {
                    object o = HttpContext.Current.Session["DataCollectionTypes"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["DataCollectionTypes"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the future primary route steps.
            /// </summary>
            /// <value>The future primary route steps.</value>
            /// <remarks>
            /// Author: patrick.konkle,   6/17/2008 - 1:00 PM
            /// Modified By: 
            /// </remarks>
            public static ArrayList FuturePrimaryRouteSteps
            {
                get
                {
                    object o = HttpContext.Current.Session["FuturePrimaryRouteSteps"];
                    return o == null ? null: (ArrayList)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["FuturePrimaryRouteSteps"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the index of the grid row.
            /// </summary>
            /// <value>The index of the grid row.</value>
            /// <remarks>
            /// Author: patrick.konkle,   6/23/2008 - 10:51 AM
            /// Modified By: 
            /// </remarks>
            public static int GridRowIndex
            {
                get
                {
                    object o = HttpContext.Current.Session["GridRowIndex"];
                    return o == null ? -1 : (int)o;
                }
                set
                {
                    HttpContext.Current.Session["GridRowIndex"] = value;
                }
            }


            /// <summary>
            /// Gets or sets a value indicating whether [BTN unit not in list display].
            /// </summary>
            /// <value>
            /// 	<c>true</c> if [BTN unit not in list display]; otherwise, <c>false</c>.
            /// </value>
            /// <remarks>
            /// Author: patrick.konkle,   7/28/2008 - 2:59 PM
            /// Modified By: 
            /// </remarks>
            public static bool btnUnitNotInListDisplay
            {
                get
                {
                    object o = HttpContext.Current.Session["btnUnitNotInListDisplay"];
                    return o == null ? false : (Boolean)o;
                }
                set
                {
                    HttpContext.Current.Session["btnUnitNotInListDisplay"] = value;
                }
            }


            /// <summary>
            /// Gets or sets a value indicating whether a button is clicked.
            /// </summary>
            /// <value><c>true</c> if [button clicked]; otherwise, <c>false</c>.</value>
            /// <remarks>
            /// Author: patrick.konkle,   7/25/2008 - 8:25 AM
            /// Modified By: 
            /// </remarks>
            public static bool ButtonClicked
            {
                get
                {
                    object o = HttpContext.Current.Session["ButtonClicked"];
                    return o == null ? false : (Boolean)o;
                }
                set
                {
                    HttpContext.Current.Session["ButtonClicked"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the last scanned identifier entry id.
            /// </summary>
            /// <value>The last scanned identifier entry id.</value>
            /// <remarks>
            /// Author: patrick.konkle,   8/8/2008 - 12:56 PM
            /// Modified By: 
            /// </remarks>
            public static string LastScannedIdentifierEntryId
            {
                get
                {
                    object o = HttpContext.Current.Session["LastScannedIdentifierEntryId"];
                    return o == null ? "" : (string)o;
                }
                set
                {
                    if (value == null) return;
                    HttpContext.Current.Session["LastScannedIdentifierEntryId"] = value;
                }
            }

            /// <summary>
            /// Gets or sets the identifier entry id backcolor.
            /// </summary>
            /// <value>The identifier entry id backcolor.</value>
            /// <remarks>
            /// Author: patrick.konkle,   8/8/2008 - 1:02 PM
            /// Modified By: 
            /// </remarks>
            public static System.Drawing.Color IdentifierEntryIdBackcolor
            {
                get
                {
                    object o = HttpContext.Current.Session["IdentifierEntryIdBackcolor"];
                    return o == null ? System.Drawing.Color.Empty : (System.Drawing.Color)o;
                }
                set
                {
                    if (value == System.Drawing.Color.Empty) return;
                    HttpContext.Current.Session["IdentifierEntryIdBackcolor"] = value;
                }
            }

            /// <summary>
            /// Alert object for informational, warning, and error messages.
            /// </summary>
            protected IDictionary<string, string> AlertObj;

            /// <summary>
            /// Gets or sets the type of the alert--informational, warning, error.
            /// </summary>
            /// <value>The type of the alert.</value>
            /// <remarks>
            /// Author: patrick.konkle,   8/6/2008 - 10:31 AM
            /// Modified By: 
            /// </remarks>
            public string AlertType
            {
                get
                {
                    if (HttpContext.Current != null)
                    {
                        AlertObj = (IDictionary<string, string>)HttpContext.Current.Session["AlertObj"];
                        if (AlertObj == null) return null;
                        return Convert.ToString(AlertObj["AlertType"]);
                    }
                    return null;
                }
                set
                {
                    if (string.IsNullOrEmpty(value)) return;

                    AlertObj = (IDictionary<string, string>)HttpContext.Current.Session["AlertObj"];
                    AlertObj["AlertType"] = value;
                    HttpContext.Current.Session["AlertObj"] = AlertObj;
                }
            }

            /// <summary>
            /// Gets or sets the alert id.
            /// NOTE This will override the Alert message, e.g. Alert Id
            /// NOTE will have an associated, localized message.
            /// </summary>
            /// <value>The alert id.</value>
            /// <remarks>
            /// Author: patrick.konkle,   8/6/2008 - 10:32 AM
            /// Modified By: 
            /// </remarks>
            public string AlertId
            {
                get
                {
                    if (HttpContext.Current != null)
                    {
                        AlertObj = (IDictionary<string, string>)HttpContext.Current.Session["AlertObj"];
                        if (AlertObj == null) return null;
                        return Convert.ToString(AlertObj["AlertId"]);
                    }
                    return null;
                }
                set
                {
                    if (string.IsNullOrEmpty(value)) return;

                    AlertObj = (IDictionary<string, string>)HttpContext.Current.Session["AlertObj"];
                    AlertObj["AlertId"] = value;
                    HttpContext.Current.Session["AlertObj"] = AlertObj;
                }
            }

            /// <summary>
            /// Gets or sets the alert message.
            /// </summary>
            /// <value>The alert MSG.</value>
            /// <remarks>
            /// Author: patrick.konkle,   8/6/2008 - 10:34 AM
            /// Modified By: 
            /// </remarks>
            public string AlertMsg
            {
                get
                {
                    if (HttpContext.Current != null)
                    {
                        AlertObj = (IDictionary<string, string>)HttpContext.Current.Session["AlertObj"];
                        if (AlertObj == null) return null;
                        return Convert.ToString(AlertObj["AlertMsg"]);
                    }
                    return null;
                }
                set
                {
                    if (string.IsNullOrEmpty(value)) return;

                    AlertObj = (IDictionary<string, string>)HttpContext.Current.Session["AlertObj"];
                    AlertObj["AlertMsg"] = value;
                    HttpContext.Current.Session["AlertObj"] = AlertObj;
                }
            }

            /// <summary>
            /// Gets or sets the reference id route type.
            /// </summary>
            /// <value>The type of the reference id route.</value>
            /// <remarks>
            /// Author: patrick.konkle,   8/26/2008 - 4:28 PM
            /// Modified By: 
            /// </remarks>
            public static int? ReferenceIdRouteType
            {
                get
                {
                    object o = HttpContext.Current.Session["ReferenceIdRouteType"];
                    return o == null ? -1 : (int)o;
                }
                set
                {
                    HttpContext.Current.Session["ReferenceIdRouteType"] = value;
                }
            }

        }
    }
}


